package com.wan37.logic.guild.util;

import com.wan37.logic.guild.domain.vo.GuildUserVO;
import com.wan37.logic.guild.domain.vo.GuildVO;
import com.wan37.logic.guild.domain.vo.GuildWarehouseVO;
import com.wan37.logic.handler.LogicServerHandler;
import com.wan37.protobuf.constant.PtoNum;
import com.wan37.protobuf.message.client2server.game.GameGuild;
import com.wan37.protobuf.message.internal.Internal;
import com.wan37.protobuf.util.ServerProtoUtils;
import io.netty.buffer.ByteBuf;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author : luoyong
 * @date : 2020-08-03 16:37
 **/
@Component
public class GuildRouteUtil {
    public void sendGuildResponse(int code, String userId, GuildVO guildVO, List<GuildUserVO> guildUserVOList,
                                  List<GuildWarehouseVO> guildWarehouseVOList,
                                  List<GameGuild.SimpleAllGuild> simpleAllGuildList){
        GameGuild.GuildResponse.Builder grb = GameGuild.GuildResponse.newBuilder();
        grb.setCode(code);
        if (guildVO != null){
            GameGuild.Guild.Builder gb = GameGuild.Guild.newBuilder();
            gb.setGuildId(guildVO.getGuildId());
            gb.setName(guildVO.getName());
            gb.setHeaderId(guildVO.getHeaderId());
            gb.setLevel(guildVO.getLevel());
            gb.setExperience(guildVO.getLevel());
            gb.setNotice(guildVO.getNotice());
            grb.setGuild(gb.build());
        }
        if (simpleAllGuildList != null){
            for (GameGuild.SimpleAllGuild simpleAllGuild : simpleAllGuildList){
                grb.addSimpleAllGuild(simpleAllGuild);
            }
        }
        if (guildUserVOList != null){
            for (GuildUserVO guildUserVO : guildUserVOList){
                GameGuild.GuildUser.Builder gub = GameGuild.GuildUser.newBuilder();
                gub.setGuildId(guildUserVO.getGuildId());
                gub.setUserId(guildUserVO.getUserId());
                gub.setJob(guildUserVO.getJob());
                grb.addGuildUser(gub.build());
            }
        }
        if (guildWarehouseVOList != null){
            for (GuildWarehouseVO guildWarehouseVO : guildWarehouseVOList){
                GameGuild.GuildWarehouse.Builder gwb = GameGuild.GuildWarehouse.newBuilder();
                gwb.setGuildId(guildWarehouseVO.getGuildId());
                gwb.setBackpackId(guildWarehouseVO.getBackpackId());
                grb.addGuildWarehouse(gwb.build());
            }
        }
        ByteBuf byteBuf = ServerProtoUtils.pack2Server(Internal.DestType.Client,
                userId, PtoNum.GUILDRESPONSE, grb.build());
        LogicServerHandler.getGateConnCtx().writeAndFlush(byteBuf);
    }

    public void sendGuildResponse(int code, String userId, GuildVO guildVO, List<GuildUserVO> guildUserVOList,
                                  List<GuildWarehouseVO> guildWarehouseVOList){
        GameGuild.GuildResponse.Builder grb = GameGuild.GuildResponse.newBuilder();
        grb.setCode(code);
        if (guildVO != null){
            GameGuild.Guild.Builder gb = GameGuild.Guild.newBuilder();
            gb.setGuildId(guildVO.getGuildId());
            gb.setName(guildVO.getName());
            gb.setHeaderId(guildVO.getHeaderId());
            gb.setLevel(guildVO.getLevel());
            gb.setExperience(guildVO.getLevel());
            gb.setNotice(guildVO.getNotice());
            grb.setGuild(gb.build());
        }
        if (guildUserVOList != null){
            for (GuildUserVO guildUserVO : guildUserVOList){
                GameGuild.GuildUser.Builder gub = GameGuild.GuildUser.newBuilder();
                gub.setGuildId(guildUserVO.getGuildId());
                gub.setUserId(guildUserVO.getUserId());
                gub.setJob(guildUserVO.getJob());
                grb.addGuildUser(gub.build());
            }
        }
        if (guildWarehouseVOList != null){
            for (GuildWarehouseVO guildWarehouseVO : guildWarehouseVOList){
                GameGuild.GuildWarehouse.Builder gwb = GameGuild.GuildWarehouse.newBuilder();
                gwb.setGuildId(guildWarehouseVO.getGuildId());
                gwb.setBackpackId(guildWarehouseVO.getBackpackId());
                grb.addGuildWarehouse(gwb.build());
            }
        }
        ByteBuf byteBuf = ServerProtoUtils.pack2Server(Internal.DestType.Client,
                userId, PtoNum.GUILDRESPONSE, grb.build());
        LogicServerHandler.getGateConnCtx().writeAndFlush(byteBuf);
    }

    public void sendGuildResponse(int code, String userId, String content){
        GameGuild.GuildResponse.Builder grb = GameGuild.GuildResponse.newBuilder();
        grb.setCode(code);
        if (content != null){
            grb.setString(content);
        }
        ByteBuf byteBuf = ServerProtoUtils.pack2Server(Internal.DestType.Client,
                userId, PtoNum.GUILDRESPONSE, grb.build());
        LogicServerHandler.getGateConnCtx().writeAndFlush(byteBuf);
    }

    public void sendGuildResponse(int code, String userId, String content, GuildVO guildVO){
        GameGuild.GuildResponse.Builder grb = GameGuild.GuildResponse.newBuilder();
        grb.setCode(code);
        if (content != null){
            grb.setString(content);
        }
        if (guildVO != null){
            GameGuild.Guild.Builder gb = GameGuild.Guild.newBuilder();
            gb.setGuildId(guildVO.getGuildId());
            gb.setName(guildVO.getName());
            gb.setHeaderId(guildVO.getHeaderId());
            gb.setLevel(guildVO.getLevel());
            gb.setExperience(guildVO.getLevel());
            gb.setNotice(guildVO.getNotice());
            grb.setGuild(gb.build());
        }
        ByteBuf byteBuf = ServerProtoUtils.pack2Server(Internal.DestType.Client,
                userId, PtoNum.GUILDRESPONSE, grb.build());
        LogicServerHandler.getGateConnCtx().writeAndFlush(byteBuf);
    }

    public void sendGuildResponse(int code, String userId, String content, List<GuildUserVO> guildUserVOList){
        GameGuild.GuildResponse.Builder grb = GameGuild.GuildResponse.newBuilder();
        grb.setCode(code);
        if (content != null){
            grb.setString(content);
        }
        if (guildUserVOList != null){
            for (GuildUserVO guildUserVO : guildUserVOList){
                GameGuild.GuildUser.Builder gub = GameGuild.GuildUser.newBuilder();
                gub.setGuildId(guildUserVO.getGuildId());
                gub.setUserId(guildUserVO.getUserId());
                gub.setJob(guildUserVO.getJob());
                grb.addGuildUser(gub.build());
            }
        }
        ByteBuf byteBuf = ServerProtoUtils.pack2Server(Internal.DestType.Client,
                userId, PtoNum.GUILDRESPONSE, grb.build());
        LogicServerHandler.getGateConnCtx().writeAndFlush(byteBuf);
    }

    public void sendGuildResponse(int code, String userId, List<String> applyUserIdList){
        GameGuild.GuildResponse.Builder grb = GameGuild.GuildResponse.newBuilder();
        grb.setCode(code);
        for (String userId1 : applyUserIdList){
            grb.addApplyUserId(GameGuild.StringMsg.newBuilder().setStringId(userId1).build());
        }
        ByteBuf byteBuf = ServerProtoUtils.pack2Server(Internal.DestType.Client,
                userId, PtoNum.GUILDRESPONSE, grb.build());
        LogicServerHandler.getGateConnCtx().writeAndFlush(byteBuf);
    }

}
